package pers.hl.module_main.common

import android.animation.Animator
import android.content.Context
import android.view.Gravity
import android.view.View
import androidx.appcompat.widget.AppCompatImageView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import per.goweii.anylayer.AnimatorHelper
import per.goweii.anylayer.AnyLayer
import per.goweii.anylayer.DragLayout
import per.goweii.anylayer.Layer
import pers.hl.lib_global.common.GlideUtils
import pers.hl.lib_global.model.enums.FileType
import pers.hl.module_main.R
import pers.hl.module_main.databinding.LayoutQrCodeBinding

/**
 * 通用底部列表弹框
 */
object CommonDialog {

    fun <T> createBottomList(
        context: Context,
        list: MutableList<T>,
        layoutResId: Int ?= null,
        convertBlock: (holder: BaseViewHolder, item: T) -> Unit,
        onItemClick: (Layer, adapter: BaseQuickAdapter<T, BaseViewHolder>, position: Int, item: T) -> Unit,
    ): Layer {
        return AnyLayer.dialog(context)
            .contentView(R.layout.layout_popup_list)
            .cancelableOnTouchOutside(true)
            .backgroundDimAmount(0.5f)
            .gravity(Gravity.BOTTOM)
            .dragDismiss(DragLayout.DragStyle.Bottom)
            .contentAnimator(object : Layer.AnimatorCreator {
                override fun createInAnimator(target: View): Animator {
                    return AnimatorHelper.createBottomInAnim(target)
                }

                override fun createOutAnimator(target: View): Animator {
                    return AnimatorHelper.createBottomOutAnim(target)
                }
            })
            .bindData { layer ->
                val rv: RecyclerView = layer.getView(R.id.rvPop)
                val resId: Int = layoutResId ?: R.layout.base_popup_item_list
                val itemAdapter = object :
                    BaseLibQuickAdapter<T, BaseViewHolder>(resId, list) {
                    override fun convert(holder: BaseViewHolder, item: T) {
                        convertBlock(holder, item)
                    }
                }
                rv.layoutManager = LinearLayoutManager(context)
                rv.adapter = itemAdapter
                itemAdapter.setOnItemClickListener { _, _, position ->
                    val item = itemAdapter.getItem(position)
                    onItemClick(layer, itemAdapter, position, item)
                }
            }
    }

    fun createCommon(
        context: Context,
        layoutResId: Int,
        gravity: Int,
        bindDataBlock: (Layer) -> Unit
    ): Layer {
        return AnyLayer.dialog(context)
            .contentView(layoutResId)
            .cancelableOnTouchOutside(true)
            .backgroundDimAmount(0.5f)
            .gravity(gravity)
            .contentAnimator(object : Layer.AnimatorCreator {
                override fun createInAnimator(target: View): Animator {
                    return AnimatorHelper.createAlphaInAnim(target)
                }

                override fun createOutAnimator(target: View): Animator {
                    return AnimatorHelper.createAlphaOutAnim(target)
                }
            })
            .bindData { layer ->
                bindDataBlock(layer)
            }
    }

    // 二维码图片弹框
    fun createQrCodeDialog(context: Context, fileType: Int, bussId: Int): Layer {
        return AnyLayer.dialog(context)
            .contentView(R.layout.layout_qr_code)
            .cancelableOnTouchOutside(true)
            .backgroundDimAmount(0.5f)
            .gravity(Gravity.CENTER)
            .contentAnimator(object : Layer.AnimatorCreator {
                override fun createInAnimator(target: View): Animator {
                    return AnimatorHelper.createAlphaInAnim(target)
                }

                override fun createOutAnimator(target: View): Animator {
                    return AnimatorHelper.createAlphaOutAnim(target)
                }
            })
            .bindData { layer ->
                val binding = LayoutQrCodeBinding.bind(layer.getView(R.id.root_qr_code))
//                val ivQrCode = layer.getView<AppCompatImageView>(R.id.ivQrCode)
                binding.apply {
                    when (fileType) {
                        FileType.BOOK_QR_CODE.value() -> {
                            tvTitle.text = "请扫描二维码以借阅此书籍"
                            GlideUtils.loadBookQrCode(context, ivQrCode, bussId)
                        }
                        FileType.USER_QR_CODE.value() -> {
                            tvTitle.text = "扫描二维码关注我吧"
                            GlideUtils.loadUserQrCode(context, ivQrCode, bussId)
                        }
                    }

                }
            }
    }
}